package com.hm.cms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hm.cms.auto.su.BaseDomain;
import com.hm.cms.domain.BlacklistDomain;
import com.hm.cms.domain.QBlacklistDomain;
import com.hm.cms.repository.BlacklistRepository;
import com.hm.cms.su.BeanUtil;
import com.hm.cms.vo.HmcmsBlacklistVo;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.util.CommonUtil;

/**
 * @author shishun.wang
 * @date 下午2:37:56 2017年11月14日
 * @version 1.0
 * @describe
 */
@Service
public class BlacklistServiceImpl implements BlacklistService {

	@Autowired
	private BlacklistRepository blacklistRepository;

	@Override
	public void addBlacklist(HmcmsBlacklistVo blacklistVo, String operationUserId) {
		BlacklistDomain domain = new BlacklistDomain();
		{
			BeanUtil.copyProperties(blacklistVo, domain);
			BaseDomain.initBaseInfo(domain);
		}
		blacklistRepository.save(domain);
	}

	@Override
	public boolean existsBlacklistByDomainName(String domainName) {
		if (CommonUtil.isEmpty(domainName)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		QBlacklistDomain qBlacklistDomain = QBlacklistDomain.blacklistDomain;
		return blacklistRepository
				.exists(qBlacklistDomain.domainName.eq(domainName).and(qBlacklistDomain.open.eq(true)));
	}

	@Override
	public boolean existsBlacklistByIp(String ip) {
		if (CommonUtil.isEmpty(ip)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}
		QBlacklistDomain qBlacklistDomain = QBlacklistDomain.blacklistDomain;
		return blacklistRepository.exists(qBlacklistDomain.ip.eq(ip).and(qBlacklistDomain.open.eq(true)));
	}

}
